package com.garlic.face.jlf.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.ServiceInterface;
import com.garlic.face.jlf.transaction.SocketService;
import com.garlic.face.jlf.transaction.constances.JLFConstant;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by shiyanqi on 18/8/20.
 */

public class CashierLoginOutActivity extends BaseActivity {

    private EditText mCashier;
    private EditText mCashierPwd;
    private EditText mSupervisor;
    private EditText mSupervisorPwd;
    private Button mBtnClose;
    private Button btnOk;
    private LinearLayout mLogoutFragment;
    private InputMethodManager inputMethodManager; // 软键盘
    private static SocketService mSocketService;
    private LinearLayout logoutLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ceshier_login_out);
        mLogoutFragment = findViewById(R.id.logout_layout);
        btnOk = findViewById(R.id.logout_OK_button);
        mBtnClose = findViewById(R.id.logout_close);
        mCashier = findViewById(R.id.cashier_logout);
        mCashierPwd = findViewById(R.id.cashierPwd_logout);
        mSupervisor = findViewById(R.id.supervisor_logout);
        mSupervisorPwd = findViewById(R.id.supervisorPwd_logout);
        logoutLayout = findViewById(R.id.logout_layout);
        mCashier.setText(Means.getInstance().getLoginCashier());
        mCashierPwd.setText(Means.getInstance().getLoginCashierPwd());
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mCashier.setInputType(InputType.TYPE_CLASS_NUMBER);
//        mCashierPwd.setInputType(InputType.TYPE_CLASS_NUMBER);
        mSupervisor.setInputType(InputType.TYPE_CLASS_NUMBER);
//        mSupervisorPwd.setInputType(InputType.TYPE_CLASS_NUMBER);

        //mCashier.setText("1018");
        String name = (String) Tools.getPref(App.CONFIG_INFO, App.CASH_NAME, "");
        if (!TextUtils.isEmpty(name)) {
            mCashier.setText(name);
        }

		/*mCashierPwd.setText("1234");
        mSupervisor.setText("4000");
		mSupervisorPwd.setText("1234");*/
        EventBus.getDefault().register(this);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!Means.getInstance().isLoginServer()) {
                    ToastUtils.toastShow("请先登录", ToastUtils.ERROR);
                    finish();
                    return;
                }
                cashierLogout();
            }
        });


        mBtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        logoutLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftInput();
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void cashierLogout() {
        ServiceInterface.getInstance().logout(mCashier.getText().toString().trim(), mCashierPwd.getText().toString().trim(), mSupervisor.getText().toString().trim(), mSupervisorPwd.getText().toString().trim());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveResponse(JSONObject jsonObject) {
        LogUtils.d("receive msg " + jsonObject, true);
        try {
            String interfaceType = jsonObject.getString("INTERFACETYPE");
            if (interfaceType.equals(JLFConstant.TAG_LOGIN_RESPONSE)) {
                String retflag = jsonObject.getString("retflag");
                String retmsg = jsonObject.getString("retmsg");
                if (retflag.equals("2")) {
                    ToastUtils.toastShow("登出成功！", ToastUtils.SUCCESS);
                    Means.getInstance().setVoidsIntervention("");
                    Means.getInstance().setLoginCashier("");
                    Means.getInstance().setLoginCashierPwd("");
                    Means.getInstance().setLoginServer(false);
                    Tools.putPref(App.CONFIG_INFO, App.CASH_NAME, "");
                    Tools.putPref(App.CONFIG_INFO, App.CASH_PWD, "");
                    //登出成功
                    hideSoftInput();
//                    finish();
                    ServiceInterface.getInstance().releaseSocket();
//                    System.exit(1);
                    App.exitApp();
                } else {
                    //登录失败
                    ToastUtils.toastShow("登出失败！" + retmsg, ToastUtils.ERROR);
                    LogUtils.e("登出失败" + retmsg);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏软键盘
     */
    private void hideSoftInput(){
        if (inputMethodManager != null) {
            // 参数1：当前viewTree任意一个view的windowToken     参数2：没要求0即可
            inputMethodManager.hideSoftInputFromWindow(mCashier.getWindowToken(), 0);
        }
    }

}
